package org.thot.model.ge 
{
	
	/**
	 * ...
	 * @author Thot
	 *
	* */
	
	import flash.display.ShaderParameterType;
	import flash.utils.Dictionary;
	import org.thot.model.ge.GameEvent;
	
	
	public class GameEventPool
	{
		
		protected var m_data:Dictionary;
		
		
		
		public function GameEventPool()
		{
			init();
		}
		
		
		public function init():void
		{
			m_data = new Dictionary();
		}
		
		protected function getTriggerTab( p_triggerName:String ):Vector.<GameEvent>
		{
			if ( m_data[ p_triggerName ] == null )
			{
				var l_vector:Vector.<GameEvent> = new Vector.<GameEvent>();
				m_data[ p_triggerName ] = new Vector.<GameEvent>();
			}
			
			return  m_data[ p_triggerName ];
		}
		
		
		public function addGameEvent( p_data:GameEvent ):void
		{
			p_data.pool = this;
			getTriggerTab( p_data.trigger ).push( p_data );
		}
		
		public function removeGameEvent( p_data:GameEvent ):void
		{
			var l_index:int = getTriggerTab( p_data.trigger ).indexOf( p_data );
			
			if ( l_index != -1 )
			{
				getTriggerTab( p_data.trigger ).splice( l_index, 1 );
			}
			
		}
		
		public function getGameEvents():Dictionary
		{
			return m_data;
		}
		
		
		public function getGameEventsByTrigger( p_triggerName:String ):Vector.<GameEvent>
		{
			if ( m_data[ p_triggerName ] == null )
			{
				return new Vector.<GameEvent>();
			}
			else
			{
				return getTriggerTab( p_triggerName );
			}
		}
		
		public function launchTrigger( p_trigger:String ):Vector.<GameEvent>
		{
			
			var l_validEvents:Vector.<GameEvent> = new Vector.<GameEvent>();
			var l_events:Vector.<GameEvent> = getGameEventsByTrigger( p_trigger );
			var l_ge:GameEvent;
			var l_max:int = l_events.length;
			var i:int = 0;
			
			for ( ; i < l_max; i++ )
			{
				l_ge = l_events[ i ];
				l_ge.exec();
			}
			
			return l_validEvents;
		}
		
		
		public function destroy():void
		{		
			
			var l_vector:Vector.<GameEvent>;
			var l_prop:String;
			var l_ge:GameEvent;
			var i:int = 0; 
			var l_max:int = 0;
			
			
			for each( l_prop in m_data )
			{
				l_vector = getTriggerTab( l_prop );
				l_max = l_vector.length;
				
				for ( i = 0; i < l_max; i++ )
				{
					l_vector[i].destroy();
				}
				
				l_vector = null;
				
			}
			
			m_data = null;
			delete this;
		}
	
	}
	
}